
-- $Id: MinimapButton.lua 71 2009-04-06 04:43:41Z Fylgjas $

-- ========================================================================= --
-- Copyright © 2009 Thomas ROBERT (aka Fylgjas EU - Marécage de Zangar)      --
-- ========================================================================= --
-- This file is part of FyLooter.                                            --
--                                                                           --
-- FyLooter is free software: you can redistribute it and/or modify it under --
-- the terms of the GNU General Public License as published by the Free      --
-- Software Foundation, either version 3 of the License, or (at your option) --
-- any later version.                                                        --
--                                                                           --
-- FyLooter is distributed in the hope that it will be useful, but WITHOUT   --
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     --
-- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for  --
-- more details.                                                             --
--                                                                           --
-- You should have received a copy of the GNU General Public License along   --
-- with FyLooter. If not, see <http://www.gnu.org/licenses/>.                --
-- ========================================================================= --

local revision = tonumber(string.sub("$Rev: 71 $", 7, -3));

if (FyLooter.revision < revision) then
  FyLooter.revision = revision;
end

-- ========================================================================= --

-- 
-- _tooltip()
--
local function _tooltip(self)
  if (self.dragging) then return end

  GameTooltip:SetOwner(self, "ANCHOR_TOPRIGHT");
  GameTooltip:SetText("FyLooter", 1, 1, 1);
  GameTooltip:AddDoubleLine("Left Click", "Open", 1, 0.8, 0.5, 0.5, 0.8, 1);
  if (FyLooter.ADMIN) then GameTooltip:AddDoubleLine("Ctrl-Left Click", "Admin", 1, 0.8, 0.5, 0.5, 0.8, 1) end
  GameTooltip:AddDoubleLine("Right Click", "Move", 1, 0.8, 0.5, 0.5, 0.8, 1);
  GameTooltip:Show();
end

-- 
-- _update()
--
local function _update(self)
  local px, py = GetCursorPosition();
	local mx, my = Minimap:GetCenter();
	local scale  = Minimap:GetEffectiveScale();

  local a = math.rad(math.deg(math.atan2(py / scale - my, px / scale - mx)) % 360);
  local r = 80 * scale;

  self:SetPoint("CENTER", r * math.cos(a), r * math.sin(a));
end

-- 
-- _start_drag()
--
local function _start_drag(self)  
  self.dragging = true;
  self:LockHighlight();
  self:SetScript("OnUpdate", _update);

  GameTooltip:Hide();
end

-- 
-- _stop_drag()
--
local function _stop_drag(self)
  self:SetScript("OnUpdate", nil);
  self:UnlockHighlight();
  self.dragging = false;

  FyLooter.db.settings.mb_x, FyLooter.db.settings.mb_y = select(4, self:GetPoint(1));
end

-- ========================================================================= --

--
-- FyLooter:SetupMinimapButton()
--
function FyLooter:SetupMinimapButton()
  -- FyLooter.MinimapButton
  self.MinimapButton = CreateFrame("Button", "FyLooter_MinimapButton", Minimap, "MiniMapButtonTemplate");    -- FyLooter_MinimapButton
  self.MinimapButton:SetWidth(33);
  self.MinimapButton:SetHeight(33);
  self.MinimapButton:SetPoint("CENTER", FyLooter.db.settings.mb_x, FyLooter.db.settings.mb_y);
  self.MinimapButton:SetFrameStrata("MEDIUM");
  self.MinimapButton:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight", "ADD");
  self.MinimapButton:RegisterForDrag("RightButton");
  self.MinimapButton:SetScript("OnEnter", _tooltip);
  self.MinimapButton:SetScript("OnLeave", function(self) GameTooltip:Hide() end);
  self.MinimapButton:SetScript("OnClick", function(self) FyLooter:Open(IsControlKeyDown()) end);
  self.MinimapButton:SetScript("OnDragStart", _start_drag);
  self.MinimapButton:SetScript("OnDragStop", _stop_drag);

  local t = self.MinimapButton:CreateTexture("$parentIcon", "BORDER");    -- FyLooter_MinimapButtonIcon
  t:SetTexture("Interface\\TargetingFrame\\TargetDead");
  t:SetWidth(20);
  t:SetHeight(20);
  t:SetPoint("CENTER", -2, 2);

  local t = self.MinimapButton:CreateTexture(nil, "OVERLAY");
  t:SetTexture("Interface\\Minimap\\Minimap-TrackingBorder");
  t:SetWidth(52);
  t:SetHeight(52);
  t:SetPoint("TOPLEFT");
end

-- ========================================================================= --

